<?php
$inc_lib = realpath(dirname(__FILE__) . '/../../teia.lib/inc/inc_core.inc');
require_once $inc_lib;
use teia\webdev\php\core as teia;

$inc_cust = realpath(dirname(__FILE__) . '/../inc/inc_cust.inc');
require_once $inc_cust;
use teia\webdev\php\cust as cust;

$result = teia\SessionWrapper::start();

if (!$result) {
	echo "Session Error: Failed to start session\n";
}
$err_msg = isset($_REQUEST['err_msg']) ? $_REQUEST['err_msg'] : '';

$prod_obj = null;
try {
	$prod_it_track = teia\SessionWrapper::get_prod_it_track();
	if (isset($_REQUEST['prod_id'])) {
		$prod_id = $_REQUEST['prod_id'];
		$prod_obj = teia\Product::db_fetch($prod_id);
		$pass = true;
		if (isset($_REQUEST['field']) && isset($_REQUEST['value'])) {
			$field = $_REQUEST['field'];
			$value = $_REQUEST['value'];
			$it_obj = $prod_obj->activate_item($field, $value);
			$pass = !is_null($it_obj);
		} 
		if (!$pass && isset($_REQUEST['item_id'])) {
			$pass = $prod_obj->set_active_item($_REQUEST['item_id']);
		}
		$item_obj = $prod_obj->get_active_item();
		$item_id = $item_obj->get_item_id();
		$prod_it_track->push_prod_it($prod_id, $item_id, 1, 0);
	} else {
		$err_msg = 'Could not load product: No product specified!';
	}	
} catch (Exception $e) {
	$err_msg = $e->getMessage() . ' [' . $e->getCode() . ']';
}


?>

<!DOCTYPE html>
<html>
	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
		<title><?php echo $item_obj->get_item_name(); ?></title>
		<link href="/style/layout.css" rel="stylesheet" type="text/css">
		<link href="/style/style01.css" rel="stylesheet" type="text/css">
		<link href="/style/product.css" rel="stylesheet" type="text/css">
		<link href="/style/stars.css" rel="stylesheet" type="text/css">
		<link href="/style/tabs.css" rel="stylesheet" type="text/css">
		<link href="/style/style_thumbnail_scroll.css" rel="stylesheet" type="text/css">
		<link href="form.css" rel="/style/stylesheet" type="text/css" media="screen">
		
		<?php teia\FunctionPool::include_jquery(); ?>
		<script src="http://cdn.jquerytools.org/1.2.6/full/jquery.tools.min.js"></script>
		<script type="text/javascript" src="/script/script.js"></script>
		<script type="text/javascript" src="/script/script_thumbnail_scroll.js"></script>
		<script type="text/javascript" src="/script/stars.js"></script>
		<script type="text/javascript" src="/script/autoadvance.js"></script>
	</head>
	<body>
		<div align="center" style="position: absolute; margin-left: inherit; margin-right: inherit">
			<!-- Display header logo -->
			<?php cust\CustFunctions::display_header_logo(); ?>
			<!-- Display top navigation -->
			<?php cust\CustFunctions::display_top_nav(); ?>
			<!-- Display error message, if any -->
			<?php cust\CustFunctions::display_err_msg($err_msg); ?>
			<!-- Display menu -->
			<?php cust\CustFunctions::display_menu(); ?>
			<div id="container">
				<div id="position1"> 
				</div>
				<div id="image_loader">
					<div id="top_frame">
						<div id="image_frame">
							<?php echo $prod_obj->generate_medium_img(); ?>
						</div>
						<div id="discription_frame" class="auto-style1">	
							<h3> <?php echo $item_obj->get('item_name'); ?> </h3>
							<p> <?php echo 'ID: '. $item_obj->get('item_id'); ?></p>
							<p> <?php 
								$stock = $item_obj->get('item_qty');
								echo 'Stock: '. $stock ?>
							</p>
							<p> Cor:
								<?php 
									$all_colors = $prod_obj->get_all_colors();
									$all_colors = teia\FunctionPool::array_to_assoc($all_colors, false);
									$item_color = $item_obj->get('item_color');
									$onchange = teia\LinkUtil::generate_prod_item_onchange('item_color');
									
									echo teia\HtmlUtil::generate_select($all_colors, $item_color, $onchange);
								?>
							</p>
							<p> Tamanho: 
								<?php 
									$all_sizes = $prod_obj->get_all_sizes();
									$all_sizes = teia\FunctionPool::array_to_assoc($all_sizes, false);
									$item_size = $item_obj->get('item_size');
									$onchange = teia\LinkUtil::generate_prod_item_onchange('item_size');
									
									echo teia\HtmlUtil::generate_select($all_sizes, $item_size, $onchange);
								?>
							</p>
							<h3><?php echo 'Preço: $'. $prod_obj->get('prod_price'); ?></h3>
							<form method="POST" action="/compra/shoppingcart.php">
								<input type="hidden" name="form_name" value="add_cart_form" />
								<input type="hidden" name="prod_id" value="<?php echo $prod_id; ?>" />
								<input type="hidden" name="item_id" value="<?php echo $item_id; ?>" />
								<input type="hidden" name="item_link" value="<?php echo teia\FunctionPool::get_request_url(); ?>" />
								<label>Quantidade: </label>
								<?php 
									$max_qty = min(array($stock, 30));	// ecom.purchase.max_it_qty = 30
									$options = teia\FunctionPool::array_to_assoc(range(1, $max_qty), false);
									echo teia\HtmlUtil::generate_select($options, 1, null, 'cart_qty');
								?>
								<br />
								<input type="image" src="/compra/img/carrinho40.gif" alt="Adicione o producto ao carrinho" name="submit" />
							</form>
							<br />
							<?php echo teia\HtmlUtil::display_rating_form(); ?> 
					</div>
					<div id="sharing">
						<?php teia\FunctionPool::include_addthis_toolbar(2); ?>
					</div>
				</div>
				
				<div id="product_discription">
					<div>
						<ul class="tabs">
							<li><a href="#">Descrição</a></li>
							<li><a href="#">Detalhes Técnicos</a></li>
							<li><a href="#">Outros</a></li>
						</ul>
						
						<div class="panes">
							<div>
								<p> <?php echo $prod_obj->get('prod_descr');?></p>
							</div>
							<div>
								<p> <?php echo $item_obj->get('item_descr');?></p>
							</div>
							<div>
								<p>...</p>
							</div>
						</div>
					</div> <!-- end tabs-->
				</div> <!-- end product_discription--->
			</div> <!-- end image_loader-->
			<div id="similar_items">
				<?php //echo $prod_obj->generate_all_thumbnails(false);
					$filter = array('prod_filter' => array(), 'item_filter' => array());
					$filter['prod_filter']['prod_type'] = $prod_obj->get('prod_type');
					$filter['prod_filter']['prod_gender'] = $prod_obj->get('prod_gender');
					$filter['item_filter']['item_size'] = $item_obj->get('item_size');
					$product_listing = new teia\ProductCatalog('_product_catalog_', 'ProductCatalog', null, $filter);
					$paginator = new teia\PaginationProvider('_pagination_', 'Pagination', null, $product_listing);
					$paginator->show_nav(false);
					@$paginator->display('page');
				?>
			</div>
	
	<div id="comment">
		<div id="content">     
	      <form method="POST" action="">
	        <div id="col1">
	        <div>
            <label for="name" >Nome:</label>
            <input name="name" id="name" type="text">
          </div>
          <div>
            <label for="email">Email:</label>
            <input name="email" id="email" type="text">
          </div>
	        </div>
	        <div id="col2">
	         <div>
            <label for="comment-text" style="vertical-align: top;">Comentário:</label>
            <textarea rows="10" cols="50" name="comment-text" id="comment-text"></textarea>
            </div> 
            <div>
          	<input type="submit" value="Submit">
        	</div>
  
	        
	        </div>

	         
	      </form>
	   </div> <!--content-->

	
	</div>



 </div>   <!--end container-->

	<?php cust\CustFunctions::display_footer();?>				
</div> <!-- end -->
		<script type="text/javascript">
			$(function() {
				$("ul.tabs").tabs("div.panes > div");
				//$( "#tabs" ).tabs({event: "mouseover"});
			});
		</script>
		<script type="text/javascript">
			$('#_medium_img').click(function() {
			  	$('#_large_img').show('slow');
			});
		</script>
	</body>
</html>